#! /usr/bin/env python2
# -*- coding: utf-8 -*-

#######################
#	获取失信人员名单 通过搜索获取,后期可通过接口传参搜索或者黄页循环获取
#######################

import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
from selenium import webdriver
from config import domains,keywords,conn
import time
import StringIO
import urllib
import re
from bs4 import BeautifulSoup
from selenium.common.exceptions import NoSuchElementException
import MySQLdb

cur = conn.cursor()

#driver = webdriver.Firefox()
print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),"Start crawling the data. . ."
driver = webdriver.PhantomJS(executable_path='/usr/local/phantomjs-2.1/bin/phantomjs')
#driver = webdriver.PhantomJS(executable_path='/usr/local/phantomjs')
print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),"Request a remote server..."
driver.get("https://www.baidu.com/s?ie=utf-8&wd=%E8%80%81%E8%B5%96")
driver.implicitly_wait(30)
driver.set_page_load_timeout(30)
driver.set_script_timeout(30)
#点击搜索
#传参
if len(sys.argv)>1:
	if "php" in sys.argv:
		keyword = u"%s" % (urllib.unquote(sys.argv[1]),)
	else:
		keyword = u"%s" % (sys.argv[1],)
else:
	keyword = u"王大信"
driver.find_element_by_class_name("op_trust_pername").clear()
driver.find_element_by_class_name("op_trust_pername").send_keys(keyword)
driver.find_element_by_class_name("op_trust_btnSearch").click()
time.sleep(2)
listinfo = []
#获取详请页面内容 处理
def dealData():
	#获取当前页面内容
	soup = BeautifulSoup(driver.page_source,"html.parser")
	#获取公司或者自然人名称列表
	iname = soup.find("div",class_="op_trust_reperson").find_all("span",class_="op_trust_name")
	#获取公司或者自然人身份证或者组织机构号
	cardNum = soup.find("div",class_="op_trust_reperson").find_all("span",class_="op_trust_fl op_trust_papers")
	if iname:
		#获取详情页面
		for i in range(len(iname)):
			items={}
			trlist = soup.find("div",class_="op_trust_reperson").find_all("div",class_="op_trust_info")[i].find_all("tr")
			for tr in trlist:
				key = tr.find_all("td")[0]
				value = tr.find_all("td")[1]
				items[key.text.strip()] = value.text.strip()
			#listinfo.append(items)
			cur.execute("select * from dishonesty where iname='%s' and cardNum='%s'" % (iname[i].text,cardNum[i].text))
			isexits = cur.fetchone()
			if not isexits:
				sql = "insert into dishonesty (iname,partyTypeName,courtName,caseCode,areaName,cardNum,performance,lastmod) values('%s','%s','%s','%s','%s','%s','%s','%s')" % (iname[i].text,items.get(u"法定代表人或者负责人姓名：",""),items.get(u"执行法院："),items.get(u"案号："),items.get(u"省份："),cardNum[i].text,items.get(u"被执行人的履行情况："),items.get(u"发布时间："))
				try:
					cur.execute(sql)
					conn.commit()
				except MySQLdb.Error,e:
					print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),"Insert failed..."
			else:
				print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),"Data duplication, insert failure..."
	else:
		print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),"Not found the company records..."
print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),"Data processing..."
dealData()
print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),"Processing is complete..."
driver.close()
driver.quit()
